package com.vcc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.data.entity.Otp;
import com.vcc.data.mapper.OtpMapper;
import com.vcc.service.client.vo.otp.ClientOtpVo;
import com.vcc.service.service.OtpService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.vcc.data.entity.table.OtpTableDef.OTP;

@Service
public class OtpServiceImpl extends ServiceImpl<OtpMapper, Otp> implements OtpService {

    @Override
    public Page<ClientOtpVo> paging(Page<ClientOtpVo> pageable, Long userCardId) {
        QueryChain<Otp> queryChain = queryChain();
        queryChain.select(OTP.ALL_COLUMNS);
        queryChain.where(OTP.USER_CARD_ID.eq(userCardId));
        return pageAs(pageable, queryChain, ClientOtpVo.class);
    }

    @Override
    public List<ClientOtpVo> listByUserCardId(Long userCardId) {
        QueryChain<Otp> queryChain = queryChain();
        queryChain.select(OTP.ALL_COLUMNS);
        queryChain.where(OTP.USER_CARD_ID.eq(userCardId));
        //查询最近30分钟之内的
        queryChain.where(OTP.CREATE_DATE.between(
                DateUtil.offsetMinute(DateUtil.date(), -30),
                DateUtil.date()
        ));
        queryChain.orderBy(OTP.CREATE_DATE.desc());
        return queryChain.listAs(ClientOtpVo.class);
    }
}
